package com.rwhansen.humidor;

import java.awt.Color;
import java.awt.Font;

import javax.swing.AbstractButton;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.Locale;
import javax.swing.JRadioButton;


public class CigarDetailDatabasePanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	static JTextField textName;
	private static JTextField textGauge;
	private static JTextField textLength;
	private static JComboBox comboBoxCARatingYear;
	private static JButton btnCancel;
	private static JButton btnSave;
	private static JButton btnSmokeCigar;
	private static JButton btnAddCigarTo;
	private static JButton btnEditCigar;
	private static JButton btnRemoveCigar;
	private Database cigarDatabase;
	private Database humidorDatabase;
	private static CigarBrandInformation cigarBrandInformation;
	private static JComboBox comboBoxSize;
	private static JComboBox comboBoxBody;
	private static JComboBox comboBoxMadeBy;
	private static JComboBox comboBoxCountry;
	private static JComboBox comboBoxCARating;
	private static JTextArea textDescription;
	static boolean delete;
	private static JButton btnNewCigar;
	private static JTextField textMadeBy;
	private static JTextField textSize;
	private static JTextField textBody;
	private static JTextField textCountry;
	private static JTextField textCARating;
	private static JTextField textCARatingYear;
	private static JTextField textPrice;
	private static NumberFormat n;
	private JLabel lblLastModified;
	private static AbstractButton rdbtnWishList;
	private static JRadioButton rdbtnRecomended;
	private static JComboBox comboBoxBinderOrigin;
	private static JComboBox comboBoxFillerOrigin;
	private static JComboBox comboBoxWrapper;
	private static JComboBox comboBoxFiller;
	private static JComboBox comboBoxWrapperOrigin;
	private static JComboBox comboBoxBinder;
	
	public CigarDetailDatabasePanel(final Cigar cigar, final Database cigars, final Database humidor, final Database ratings) {
		cigarDatabase = cigars;
		humidorDatabase = humidor;
		delete = false;
		
		setFont(new Font("GodOfWar", Font.PLAIN, 12));
		setBounds(100, 100, 806, 684);
		setBorder(new EmptyBorder(5, 5, 5, 5));
		setLayout(null);
		
		getCigarBrandInformation();
		
//Name
		JLabel lblName = new JLabel("Cigar Name");
		lblName.setBounds(10, 55, 249, 30);
		lblName.setFont(new Font("GodOfWar", Font.PLAIN, 17));
		add(lblName);
		
		textName = new JTextField();
		textName.setBounds(259, 55, 537, 30);
		textName.setForeground(Color.BLACK);
		textName.setText(cigar.getName());
		textName.setToolTipText("Cigar Name");
		textName.setFont(new Font("GodOfWar", Font.PLAIN, 13));
		add(textName);
		textName.setColumns(10);
		
//Brand
		JLabel lblBrand = new JLabel("Brand");
		lblBrand.setBounds(10, 88, 249, 30);
		lblBrand.setFont(new Font("GodOfWar", Font.PLAIN, 17));
		add(lblBrand);
		
		comboBoxMadeBy = new JComboBox();
		comboBoxMadeBy.setEditable(true);
		comboBoxMadeBy.setMaximumRowCount(20);
		comboBoxMadeBy.setToolTipText("Cigar Brand");
		comboBoxMadeBy.setBounds(259, 88, 249, 30);
		comboBoxMadeBy.setModel(new DefaultComboBoxModel(cigarBrandInformation.getComboBoxBrands(cigar)));
		comboBoxMadeBy.setForeground(Color.BLACK);
		comboBoxMadeBy.setFont(new Font("GodOfWar", Font.PLAIN, 13));
		comboBoxMadeBy.setVisible(false);
		add(comboBoxMadeBy);
		
		textMadeBy = new JTextField();
		textMadeBy.setToolTipText("Cigar Brand");
		textMadeBy.setText(cigar.getBrand());
		textMadeBy.setForeground(Color.BLACK);
		textMadeBy.setFont(new Font("GodOfWar", Font.PLAIN, 13));
		textMadeBy.setColumns(10);
		textMadeBy.setBounds(259, 88, 249, 30);
		textMadeBy.setEditable(false);
		add(textMadeBy);
		
//Size
		JLabel lblSize = new JLabel("Size");
		lblSize.setBounds(10, 121, 249, 30);
		lblSize.setFont(new Font("GodOfWar", Font.PLAIN, 17));
		add(lblSize);
		
		comboBoxSize = new JComboBox();
		comboBoxSize.setEditable(true);
		comboBoxSize.setMaximumRowCount(20);
		comboBoxSize.setForeground(Color.BLACK);
		comboBoxSize.setToolTipText("Size/Shape of Cigar");
		comboBoxSize.setBounds(259, 121, 249, 30);
		comboBoxSize.setModel(new DefaultComboBoxModel(cigarBrandInformation.getComboBoxSizes(cigar)));
		comboBoxSize.setFont(new Font("GodOfWar", Font.PLAIN, 13));
		comboBoxSize.setVisible(false);
		add(comboBoxSize);
		
		textSize = new JTextField();
		textSize.setToolTipText("Size/Shape of Cigar");
		textSize.setText(cigar.getSize());
		textSize.setForeground(Color.BLACK);
		textSize.setFont(new Font("GodOfWar", Font.PLAIN, 13));
		textSize.setColumns(10);
		textSize.setBounds(259, 121, 249, 30);
		textSize.setEditable(false);
		add(textSize);

//Body
		JLabel lblBody = new JLabel("Body");
		lblBody.setBounds(10, 154, 249, 30);
		lblBody.setFont(new Font("GodOfWar", Font.PLAIN, 17));
		add(lblBody);

		comboBoxBody = new JComboBox();
		comboBoxBody.setMaximumRowCount(20);
		comboBoxBody.setForeground(Color.BLACK);
		comboBoxBody.setToolTipText("Cigar Body");
		comboBoxBody.setBounds(259, 154, 249, 30);
		comboBoxBody.setModel(new DefaultComboBoxModel(cigarBrandInformation.getComboBoxBodies(cigar)));
		comboBoxBody.setFont(new Font("GodOfWar", Font.PLAIN, 13));
		comboBoxBody.setVisible(false);
		add(comboBoxBody);
		
		textBody = new JTextField();
		textBody.setToolTipText("Cigar Body");
		textBody.setText(cigar.getBody());
		textBody.setForeground(Color.BLACK);
		textBody.setFont(new Font("GodOfWar", Font.PLAIN, 13));
		textBody.setColumns(10);
		textBody.setBounds(259, 154, 249, 30);
		textBody.setEditable(false);
		add(textBody);
		
//Gauge
		JLabel lblGauge = new JLabel("Gauge");
		lblGauge.setBounds(10, 187, 249, 30);
		lblGauge.setFont(new Font("GodOfWar", Font.PLAIN, 16));
		add(lblGauge);
		
		textGauge = new JFormattedTextField();
		textGauge.setForeground(Color.BLACK);
		textGauge.setText(Integer.toString(cigar.getGauge()));
		textGauge.setToolTipText("Cigar Gauge");
		textGauge.setBounds(259, 187, 249, 30);
		textGauge.setFont(new Font("GodOfWar", Font.PLAIN, 13));
		add(textGauge);
		textGauge.setColumns(10);
		
//Length
		JLabel lblLength = new JLabel("Length");
		lblLength.setBounds(10, 220, 249, 30);
		lblLength.setFont(new Font("GodOfWar", Font.PLAIN, 17));
		add(lblLength);
		
		textLength = new JFormattedTextField();
		textLength.setForeground(Color.BLACK);
		textLength.setText(Double.toString(cigar.getLength()));
		textLength.setToolTipText("Cigar Length");
		textLength.setBounds(259, 220, 249, 30);
		textLength.setFont(new Font("GodOfWar", Font.PLAIN, 13));
		add(textLength);
		textLength.setColumns(10);
		
//Country
		JLabel lblCountry = new JLabel("Country");
		lblCountry.setBounds(10, 253, 249, 30);
		lblCountry.setFont(new Font("GodOfWar", Font.PLAIN, 17));
		add(lblCountry);
		
		comboBoxCountry = new JComboBox();
		comboBoxCountry.setMaximumRowCount(20);
		comboBoxCountry.setForeground(Color.BLACK);
		comboBoxCountry.setToolTipText("Cigar Country");
		comboBoxCountry.setBounds(259, 253, 249, 30);
		comboBoxCountry.setModel(new DefaultComboBoxModel(cigarBrandInformation.getComboBoxCountries(cigar)));
		comboBoxCountry.setFont(new Font("GodOfWar", Font.PLAIN, 13));
		comboBoxCountry.setEditable(true);
		add(comboBoxCountry);
		
		textCountry = new JTextField();
		textCountry.setToolTipText("Cigar Country");
		textCountry.setText(cigar.getCountry());
		textCountry.setForeground(Color.BLACK);
		textCountry.setFont(new Font("GodOfWar", Font.PLAIN, 13));
		textCountry.setColumns(10);
		textCountry.setBounds(259, 253, 249, 30);
		textCountry.setEditable(false);
		add(textCountry);
		
//CA Rating
		JLabel lblCARating = new JLabel("Cigar Aficionado Rating");
		lblCARating.setBounds(10, 286, 249, 30);
		lblCARating.setFont(new Font("GodOfWar", Font.PLAIN, 17));
		add(lblCARating);
		
		comboBoxCARating = new JComboBox();
		comboBoxCARating.setMaximumRowCount(20);
		comboBoxCARating.setForeground(Color.BLACK);
		comboBoxCARating.setToolTipText("Cigar Aficionado Rating");
		comboBoxCARating.setBounds(259, 286, 249, 30);
		comboBoxCARating.setModel(new DefaultComboBoxModel(cigarBrandInformation.getComboBoxCARatings(cigar)));
		comboBoxCARating.setFont(new Font("GodOfWar", Font.PLAIN, 13));
		add(comboBoxCARating);
		
		textCARating = new JTextField();
		textCARating.setToolTipText("Cigar Aficionado Rating");
		textCARating.setText(Integer.toString(cigar.getCaRating()));
		textCARating.setForeground(Color.BLACK);
		textCARating.setFont(new Font("GodOfWar", Font.PLAIN, 13));
		textCARating.setColumns(10);
		textCARating.setBounds(259, 286, 249, 30);
		textCARating.setEditable(false);
		add(textCARating);
		
//CA Rating Year
		JLabel lblCaRatingYear = new JLabel("CA Rating Year");
		lblCaRatingYear.setBounds(10, 319, 249, 30);
		lblCaRatingYear.setFont(new Font("GodOfWar", Font.PLAIN, 17));
		add(lblCaRatingYear);
		
		comboBoxCARatingYear = new JComboBox();
		comboBoxCARatingYear.setMaximumRowCount(20);
		comboBoxCARatingYear.setForeground(Color.BLACK);
		comboBoxCARatingYear.setToolTipText("Cigar Aficionado Rating Year");
		comboBoxCARatingYear.setBounds(259, 319, 249, 30);
		comboBoxCARatingYear.setModel(new DefaultComboBoxModel(cigarBrandInformation.getComboBoxCARatingYears(cigar)));
		comboBoxCARatingYear.setFont(new Font("GodOfWar", Font.PLAIN, 13));
		add(comboBoxCARatingYear);
		
		textCARatingYear = new JTextField();
		textCARatingYear.setToolTipText("Cigar Aficionado Rating Year");
		textCARatingYear.setText(Integer.toString(cigar.getCaRatingYear()));
		textCARatingYear.setForeground(Color.BLACK);
		textCARatingYear.setFont(new Font("GodOfWar", Font.PLAIN, 13));
		textCARatingYear.setColumns(10);
		textCARatingYear.setBounds(259, 319, 249, 30);
		textCARatingYear.setEditable(false);
		add(textCARatingYear);
		
//Price
		n = NumberFormat.getCurrencyInstance(Locale.US); 
		
		JLabel lblAproximatePrice = new JLabel("Aproximate Price");
		lblAproximatePrice.setFont(new Font("GodOfWar", Font.PLAIN, 17));
		lblAproximatePrice.setBounds(10, 352, 239, 16);
		add(lblAproximatePrice);
		
		textPrice = new JTextField();
		textPrice.setFont(new Font("GodOfWar", Font.PLAIN, 13));
		textPrice.setText(n.format(cigar.getPrice().doubleValue()));
		textPrice.setBounds(259, 346, 249, 28);
		textPrice.setEditable(false);
		add(textPrice);
		textPrice.setColumns(10);

		
//Description
		JLabel lblDescription = new JLabel("Description:");
		lblDescription.setBounds(10, 371, 249, 30);
		lblDescription.setFont(new Font("GodOfWar", Font.PLAIN, 17));
		add(lblDescription);
		
		JScrollPane scrollPaneDescription = new JScrollPane();
		scrollPaneDescription.setBounds(11, 404, 497, 82);
		add(scrollPaneDescription);
		
		textDescription = new JTextArea();
		scrollPaneDescription.setViewportView(textDescription);
		textDescription.setForeground(Color.BLACK);
		textDescription.setLineWrap(true);
		textDescription.setWrapStyleWord(true);
		textDescription.setToolTipText("Description of Cigar");
		textDescription.setFont(new Font("GodOfWar", Font.PLAIN, 13));
		textDescription.setText(cigar.getDescription());
		
		
		
		comboBoxBinderOrigin = new JComboBox();
		comboBoxBinderOrigin.setEditable(true);
		comboBoxBinderOrigin.setModel(new DefaultComboBoxModel(new String[] {cigar.getBinderOrigin(), "Unknown Binder Origin", "Honduran"}));
		comboBoxBinderOrigin.setFont(new Font("GodOfWar", Font.PLAIN, 13));
		comboBoxBinderOrigin.setToolTipText("Binder");
		comboBoxBinderOrigin.setBounds(257, 549, 249, 28);
		add(comboBoxBinderOrigin);

		comboBoxBinder = new JComboBox();
		comboBoxBinder.setEditable(true);
		comboBoxBinder.setFont(new Font("GodOfWar", Font.PLAIN, 13));
		comboBoxBinder.setModel(new DefaultComboBoxModel(new String[] {cigar.getBinder(), "Unknown Binder"}));
		comboBoxBinder.setBounds(4, 549, 249, 28);
		add(comboBoxBinder);
		
	
		
		comboBoxFillerOrigin = new JComboBox();
		comboBoxFillerOrigin.setEditable(true);
		comboBoxFillerOrigin.setModel(new DefaultComboBoxModel(new String[] {cigar.getFillerOrigin(), "Unknown Filler Origin", "Dominican/Honduran"}));
		comboBoxFillerOrigin.setFont(new Font("GodOfWar", Font.PLAIN, 13));
		comboBoxFillerOrigin.setToolTipText("Filler");
		comboBoxFillerOrigin.setBounds(257, 613, 249, 28);
		add(comboBoxFillerOrigin);
		
		comboBoxFiller = new JComboBox();
		comboBoxFiller.setEditable(true);
		comboBoxFiller.setFont(new Font("GodOfWar", Font.PLAIN, 13));
		comboBoxFiller.setModel(new DefaultComboBoxModel(new String[] {cigar.getFiller(), "Unknown Filler"}));
		comboBoxFiller.setBounds(4, 613, 249, 28);
		add(comboBoxFiller);
		
		
		
		comboBoxWrapperOrigin = new JComboBox();
		comboBoxWrapperOrigin.setEditable(true);
		comboBoxWrapperOrigin.setFont(new Font("GodOfWar", Font.PLAIN, 13));
		comboBoxWrapperOrigin.setModel(new DefaultComboBoxModel(new String[] {cigar.getWrapperOrigin(), "Unknown Wrapper Origin"}));
		comboBoxWrapperOrigin.setBounds(257, 582, 249, 28);
		add(comboBoxWrapperOrigin);
		
		comboBoxWrapper = new JComboBox();
		comboBoxWrapper.setEditable(true);
		comboBoxWrapper.setModel(new DefaultComboBoxModel(new String[] {cigar.getWrapper(), "Unknown Wrapper", "Habano"}));
		comboBoxWrapper.setFont(new Font("GodOfWar", Font.PLAIN, 13));
		comboBoxWrapper.setToolTipText("Wrapper");
		comboBoxWrapper.setBounds(4, 582, 249, 28);
		add(comboBoxWrapper);
		
		
		
		
		
		
		
		
//Modified
		lblLastModified = new JLabel("Last Modified: " + cigar.getModified());
		lblLastModified.setFont(new Font("GodOfWar", Font.PLAIN, 10));
		lblLastModified.setBounds(259, 378, 249, 16);
		add(lblLastModified);
		
//Radio Buttons
		rdbtnWishList = new JRadioButton("Wish List");
		rdbtnWishList.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				editFields(false);
				setCigarDatabaseButtonsVisible(true, true);
				
				
				Cigar newCigar = new Cigar(textName.getText());
				BigDecimal newPrice = new BigDecimal(textPrice.getText().replace("$", ""));
				
				newCigar.setQuantity(1);
				newCigar.setBrand(comboBoxMadeBy.getSelectedItem().toString());
				newCigar.setSize(comboBoxSize.getSelectedItem().toString());
				newCigar.setBody(comboBoxBody.getSelectedItem().toString());
				newCigar.setGauge(Integer.parseInt(textGauge.getText()));
				newCigar.setLength(Double.parseDouble(textLength.getText()));
				newCigar.setCountry(comboBoxCountry.getSelectedItem().toString());
				newCigar.setCaRating(Integer.parseInt(comboBoxCARating.getSelectedItem().toString()));
				newCigar.setCaRatingYear(Integer.parseInt(comboBoxCARatingYear.getSelectedItem().toString()));
				newCigar.setPrice(newPrice);
				newCigar.setDescription(textDescription.getText());
				newCigar.setModified("T");
				newCigar.setWishList(rdbtnWishList.isSelected());
				
				cigarDatabase.addCigarToDatabase(newCigar);

				cigarDatabase.removeCigarFromDatabase(cigar);
				delete = false;

				
				Database.writeDatabase(Database.CIGAR_DB_FILENAME, cigarDatabase);
				PersonalCigarAssistant.refreshTables();
				
				PersonalCigarAssistant.tabbedPane.setSelectedComponent(PersonalCigarAssistant.cigarDatabaseScrollPane);
				PersonalCigarAssistant.cigarDatabaseTable.changeSelection(0, 0, false, false);
			}
		});
		rdbtnWishList.setSelected(cigar.isWishList());
		rdbtnWishList.setFont(new Font("GodOfWar", Font.PLAIN, 16));
		rdbtnWishList.setBounds(76, 490, 141, 23);
		rdbtnWishList.setVisible(false);
		add(rdbtnWishList);
		
		
		rdbtnRecomended = new JRadioButton("Recomended");
		rdbtnRecomended.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
//				newCigar.setRecomend(rdbtnRecomended.isSelected());
			}
		});
		rdbtnRecomended.setSelected(cigar.isRecomend());
		rdbtnRecomended.setFont(new Font("GodOfWar", Font.PLAIN, 16));
		rdbtnRecomended.setBounds(293, 490, 141, 23);
		rdbtnRecomended.setVisible(false);
		add(rdbtnRecomended);
		
		
		
		
		
		
		
//Buttons ------------------------------------------------------------------------------
		
//New Cigar Button
		btnNewCigar = new JButton("New Cigar");
		btnNewCigar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				editFields(true);
				setCigarDatabaseButtonsVisible(false, false);
				delete = false;
				newCigarDataFields();
				textName.setText("New Cigar");
				
			}
		});
		btnNewCigar.setBounds(7, 3, 160, 23);
		add(btnNewCigar);
	
//Edit Cigar Button
		btnEditCigar = new JButton("Edit Cigar");
		btnEditCigar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setCigarDatabaseButtonsVisible(false, false);
				editFields(true);
				delete = true;
			}
		});
		btnEditCigar.setBounds(174, 3, 160, 23);
		add(btnEditCigar);
	
//Add Cigar To Humidor Button
		btnAddCigarTo = new JButton("Add Cigar To Humidor");
		btnAddCigarTo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				cigar.setDateAddHumidor("T");
				cigar.setHumidor(humidorDatabase.getName());
				cigar.setPurchaseLocation("Unknown Purchase Location");
				
				PersonalCigarAssistant.buildHumidorDetails(cigar);
				CigarDetailHumidorPanel.editFields(true);
				CigarDetailHumidorPanel.setHumidorButtonsEnabled(false, false, false);
			}
		});
		btnAddCigarTo.setBounds(11, 29, 238, 23);
		add(btnAddCigarTo);
		
//Smoke Cigar Button
		btnSmokeCigar = new JButton("Smoke Cigar");
		btnSmokeCigar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				cigar.setHumidor("Not Kept");
				cigar.setDateAddHumidor("T");
				cigar.setDateSmoked("T");
				cigar.setPurchaseLocation("Unknown Purchase Location");
				
				PersonalCigarAssistant.buildRatingDetails(cigar);
				CigarDetailRatingPanel.editFields(true);
			}
		});
		btnSmokeCigar.setBounds(260, 29, 238, 23);
		add(btnSmokeCigar);
		
//Cancel Button
		btnCancel = new JButton("Cancel");
//		btnCancel.setEnabled(false);
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				cigar.setHumidor("");
				cigar.setDateAddHumidor("0000/00/00");
				cigar.setDateSmoked("0000/00/00");
				
				editFields(false);
				setCigarDatabaseButtonsVisible(true, true);
				delete = false;
				fillDataFields(cigar);
				
				PersonalCigarAssistant.tabbedPane.setSelectedComponent(PersonalCigarAssistant.cigarDatabaseScrollPane);
				PersonalCigarAssistant.cigarDatabaseTable.changeSelection(0, 0, false, false);
				}
		});
		btnCancel.setBounds(10, 515, 238, 23);
		add(btnCancel);
		
//Save Button
		btnSave = new JButton("Save");
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				editFields(false);
				setCigarDatabaseButtonsVisible(true, true);
				
				if(textPrice.getText().isEmpty())
				{
					textPrice.setText(n.format(0));
				}
				
				
				Cigar newCigar = new Cigar(textName.getText());
				BigDecimal newPrice = new BigDecimal(textPrice.getText().replace("$", ""));
				
				newCigar.setQuantity(1);
				newCigar.setBrand(comboBoxMadeBy.getSelectedItem().toString());
				newCigar.setSize(comboBoxSize.getSelectedItem().toString());
				newCigar.setBody(comboBoxBody.getSelectedItem().toString());
				newCigar.setGauge(Integer.parseInt(textGauge.getText()));
				newCigar.setLength(Double.parseDouble(textLength.getText()));
				newCigar.setCountry(comboBoxCountry.getSelectedItem().toString());
				newCigar.setCaRating(Integer.parseInt(comboBoxCARating.getSelectedItem().toString()));
				newCigar.setCaRatingYear(Integer.parseInt(comboBoxCARatingYear.getSelectedItem().toString()));
				newCigar.setPrice(newPrice);
				newCigar.setDescription(textDescription.getText());
				newCigar.setModified("T");
				newCigar.setFullySmoked("Unknown");
				
				newCigar.setBinder(comboBoxBinder.getSelectedItem().toString());
				newCigar.setBinderOrigin(comboBoxBinderOrigin.getSelectedItem().toString());
				newCigar.setFiller(comboBoxFiller.getSelectedItem().toString());
				newCigar.setFillerOrigin(comboBoxFillerOrigin.getSelectedItem().toString());
				newCigar.setWrapper(comboBoxWrapper.getSelectedItem().toString());
				newCigar.setWrapperOrigin(comboBoxWrapperOrigin.getSelectedItem().toString());
				
				 
				
				cigarDatabase.addCigarToDatabase(newCigar);
				
				if (delete) {
					cigarDatabase.removeCigarFromDatabase(cigar);
					delete = false;
				}
				
				Database.writeDatabase(Database.CIGAR_DB_FILENAME, cigarDatabase);
				PersonalCigarAssistant.refreshTables();
				
				PersonalCigarAssistant.tabbedPane.setSelectedComponent(PersonalCigarAssistant.cigarDatabaseScrollPane);
//				PersonalCigarAssistant.cigarDatabaseTable.changeSelection(0, 0, false, false);
			}
		});
//		btnSave.setEnabled(false);
		btnSave.setBounds(259, 515, 238, 23);
		add(btnSave);
		
//Remove Cigar Button
		btnRemoveCigar = new JButton("Remove Cigar");
		btnRemoveCigar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				PersonalCigarAssistant.setWindowEnabled(false);
				RemoveCigarFromDatabase remove = new RemoveCigarFromDatabase(cigar, cigarDatabase.getCigars().indexOf(cigar), Database.CIGAR_DB_FILENAME);
				remove.setVisible(true);
			}
		});
		btnRemoveCigar.setBounds(341, 3, 160, 23);
		add(btnRemoveCigar);
		
		
		
		
		
		editFields(false);
	}
	
//	/**
//	 * If visible is set true then the save and cancel buttons will be enabled, if false not enabled
//	 * @param visible
//	 */
//	public void setEditingButtonsVisible(boolean visible) {
//		btnCancel.setEnabled(visible);
//		btnSave.setEnabled(visible);
//		btnRemoveCigar.setEnabled(visible);
//		btnEditCigar.setEnabled(visible);
//		btnSmokeCigar.setEnabled(visible);
//	}
	
	public static void newCigarDataFields() {
		Cigar newCigar = new Cigar("New Cigar");
		newCigar.setNew();
		textName.setText(newCigar.getName());
		comboBoxMadeBy.setModel(new DefaultComboBoxModel(cigarBrandInformation.getComboBoxBrands(newCigar)));
		comboBoxSize.setModel(new DefaultComboBoxModel(cigarBrandInformation.getComboBoxSizes(newCigar)));
		comboBoxBody.setModel(new DefaultComboBoxModel(cigarBrandInformation.getComboBoxBodies(newCigar)));
		textGauge.setText(Integer.toString(newCigar.getGauge()));
		textLength.setText(Double.toString(newCigar.getLength()));
		comboBoxCountry.setModel(new DefaultComboBoxModel(cigarBrandInformation.getComboBoxCountries(newCigar)));
		comboBoxCARating.setModel(new DefaultComboBoxModel(cigarBrandInformation.getComboBoxCARatings(newCigar)));
		comboBoxCARatingYear.setModel(new DefaultComboBoxModel(cigarBrandInformation.getComboBoxCARatingYears(newCigar)));
		textDescription.setText(newCigar.getDescription());
		textPrice.setText(n.format(newCigar.getPrice().doubleValue()));
	}
	
	public void fillDataFields(Cigar cigar) {
		textName.setText(cigar.getName());
		comboBoxMadeBy.setModel(new DefaultComboBoxModel(cigarBrandInformation.getComboBoxBrands(cigar)));
		comboBoxSize.setModel(new DefaultComboBoxModel(cigarBrandInformation.getComboBoxSizes(cigar)));
		comboBoxBody.setModel(new DefaultComboBoxModel(cigarBrandInformation.getComboBoxBodies(cigar)));
		textGauge.setText(Integer.toString(cigar.getGauge()));
		textLength.setText(Double.toString(cigar.getLength()));
		comboBoxCountry.setModel(new DefaultComboBoxModel(cigarBrandInformation.getComboBoxCountries(cigar)));
		comboBoxCARating.setModel(new DefaultComboBoxModel(cigarBrandInformation.getComboBoxCARatings(cigar)));
		comboBoxCARatingYear.setModel(new DefaultComboBoxModel(cigarBrandInformation.getComboBoxCARatingYears(cigar)));
		textDescription.setText(cigar.getDescription());
	}
	
	/**
	 * This is what populates all of the combobox drop downs
	 */
	public void getCigarBrandInformation() {
		String cigarBrandInformationFileName = CigarBrandInformation.CBI_FILE_NAME;
		cigarBrandInformation = new CigarBrandInformation();
		File cbiFile = new File(cigarBrandInformationFileName);
		boolean cbiExists = cbiFile.exists();
		if (cbiExists){
			cigarBrandInformation = CigarBrandInformation.readCigarBrandInformation(cigarBrandInformationFileName);
		}
	}

	/**
	 * Sets the buttons in the Humidor detail pane visible and not visible
	 * 
	 * @param visible
	 * @param newCigar 
	 */
	public static void setCigarDatabaseButtonsVisible(boolean visible, boolean newCigarBtn) {
		btnRemoveCigar.setEnabled(visible);
		btnEditCigar.setEnabled(visible);
		btnSmokeCigar.setEnabled(visible);
		btnAddCigarTo.setEnabled(visible);
		rdbtnWishList.setVisible(false);
		rdbtnRecomended.setVisible(false);
		btnNewCigar.setEnabled(newCigarBtn);
	}
	
	/**
	 * This will allow the text fields to be edited or no not.
	 * @param edit True - Fields can be edited; False - Fields can not be edited.
	 */
	public static void editFields(boolean edit) {
		boolean initialyVisibleTrue = true;
		boolean initialyVisibleFalse = false;
		if (edit == true) {
			initialyVisibleTrue = false;
			initialyVisibleFalse = true;
		}
		
		textName.setEditable(initialyVisibleFalse);
		comboBoxMadeBy.setVisible(initialyVisibleFalse);
		textMadeBy.setVisible(initialyVisibleTrue);
		comboBoxSize.setVisible(initialyVisibleFalse);
		textSize.setVisible(initialyVisibleTrue);
		comboBoxBody.setVisible(initialyVisibleFalse);
		textBody.setVisible(initialyVisibleTrue);
		comboBoxCountry.setVisible(initialyVisibleFalse);
		textCountry.setVisible(initialyVisibleTrue);
		comboBoxCARating.setVisible(initialyVisibleFalse);
		textCARating.setVisible(initialyVisibleTrue);
		comboBoxCARatingYear.setVisible(initialyVisibleFalse);
		textCARatingYear.setVisible(initialyVisibleTrue);
		textGauge.setEditable(initialyVisibleFalse);
		textLength.setEditable(initialyVisibleFalse);
		textDescription.setEditable(initialyVisibleFalse);
		textPrice.setEditable(initialyVisibleFalse);
		
		comboBoxBinderOrigin.setEnabled(initialyVisibleFalse);
		comboBoxBinder.setEnabled(initialyVisibleFalse);
		comboBoxFillerOrigin.setEnabled(initialyVisibleFalse);
		comboBoxFiller.setEnabled(initialyVisibleFalse);
		comboBoxWrapperOrigin.setEnabled(initialyVisibleFalse);
		comboBoxWrapper.setEnabled(initialyVisibleFalse);

		
		btnCancel.setVisible(initialyVisibleFalse);
		btnSave.setVisible(initialyVisibleFalse);
	}
}
